<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="98px">
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品明细" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入产品明细"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否自制" prop="isdo">
        <el-select v-model="queryParams.isdo" placeholder="是否自制" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.isdo"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="addGoods()">新增产品</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading"
              show-summary :summary-method="getSummaries"  :max-height="maxHeight"
              @sort-change="handleSortChange">
        <el-table-column align="center" label="序号" type="index" width="50">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column sortable label="订单编码" align="center" prop="projectNo" show-overflow-tooltip/>
        <el-table-column sortable label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip/>
        <el-table-column sortable label="产品编码" align="center" prop="goodsCode"/>
        <el-table-column sortable label="产品状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.goods_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column align="center" sortable label="工序分配状态" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sale_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column align="center" sortable label="产品名称" prop="goodsName" show-overflow-tooltip/>
       <el-table-column label="合同物料名称" align="center" prop="contractGoodsName" show-overflow-tooltip/>
        <el-table-column align="center" sortable label="产品型号" prop="goodModel"/>
        <el-table-column align="center" label="产品单位" prop="unit"/>
        <el-table-column align="center" label="不含税单价" prop="goodsUnitPrice"/>
        <el-table-column align="center" label="含税单价" prop="goodsTaxUnitPrice"/>
        <el-table-column align="center" label="订单数量" prop="orderCount"/>
        <el-table-column align="center" label="不含税总价" prop="goodsTotalPrice"/>
        <el-table-column align="center" label="含税总价" prop="goodsTaxTotalPrice"/>
        <el-table-column align="center" sortable prop="deliveryTime" label="需求日期"  width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" sortable label="锁定数量" prop="lockCount"/>
        <el-table-column align="center" sortable label="剩余发货" prop="fahuoShengyu"/>
        <el-table-column label="是否自制" sortable align="center" prop="isdo">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.isdo" :value="scope.row.isdo"/>
          </template>
        </el-table-column>
        <el-table-column align="center" sortable label="自制/请购数量" prop="pleaseBuyCount"/>
        <el-table-column align="center" label="变更记录" prop="modifyremark" show-overflow-tooltip />
        <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip />
<!--        <el-table-column align="center" label="巡检单" prop="xunjianFile" width="200">-->
<!--          <template slot-scope="scope">-->
<!--            <image-preview :src="scope.row.xunjianFile" height="60px" width="40px"></image-preview>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column align="center" label="备注" prop="remark"/>-->
        <el-table-column align="center" label="操作" fixed="right" width="180">
          <template slot-scope="scope">
            <!--<el-button icon="el-icon-view" size="mini" type="text"  @click="deleteByIdThree(scope.row)" v-if="scope.row.status=='0'" >
              删除产品
            </el-button>-->
            <!--<el-popconfirm :title="`确认删除产品吗？`" @confirm="deleteByIdThree(scope.row)" v-if="scope.row.status=='0'" style="margin-left: 10px;">
                <el-button icon="el-icon-view" size="mini" type="text">
                  删除产品
                </el-button>
            </el-popconfirm>-->
            <el-popconfirm :title="`确认提交当前数据项？`" @confirm="toAgain(scope.row)"  v-hasPermi="['develop:GoodsSale:delete']"
                                               v-if="scope.row.status=='0'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-upload2" >提交</el-button>
          </el-popconfirm>
            <el-popconfirm :title="`确认删除当前数据项？`" @confirm="deleteByIdThree(scope.row)"  v-hasPermi="['develop:GoodsSale:delete']"
                           v-if="scope.row.status=='0'" style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete" >删除</el-button>
            </el-popconfirm>

            <el-popconfirm :title="`确认关闭当前数据项？`" @confirm="deleteSaleFour(scope.row)"   v-show="scope.row.status!='30'"  style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete" >关闭产品</el-button>
            </el-popconfirm>
            <el-popconfirm :title="`确认关闭当前数据项？`" @confirm="deleteByIdFour(scope.row)"   v-show="scope.row.status=='30'" style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete" >退回产品关闭</el-button>
            </el-popconfirm>
            <!-- <el-button  @click="chaijieBnt(scope.row)"  icon="el-icon-setting" size="mini" type="text">
              拆解明细
            </el-button> -->
            <!--<el-button v-if="scope.row.isdo==='1'" @click="chaijie(scope.row)"  icon="el-icon-setting" size="mini" type="text">
              材料拆解
            </el-button>
            <el-button v-if="scope.row.isdo==='1'" size="mini" type="text" icon="el-icon-document"
                       @click="yongliaoMingxi(scope.row)">
              用料明细
            </el-button>
            <el-button v-if="scope.row.isdo==='1'" @click="qualityTest(scope.row)"  icon="el-icon-view" size="mini" type="text">
              质检情况
            </el-button>
            <el-button v-if="scope.row.isdo==='1'" @click="shengchanDoGoodsSale(scope.row)"  icon="el-icon-setting" size="mini" type="text">
              生产过程
            </el-button>-->
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="editRemark(scope.row)">修改备注</el-button>
            <el-button v-if="scope.row.status==='0'" size="mini" type="text" icon="el-icon-edit" @click="editGoods(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.status==='30'" size="mini" type="text" icon="el-icon-edit" @click="editGoodsTuihui(scope.row)">变更</el-button>
          </template>
        </el-table-column>
    </el-table>
    <pagination :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
      @pagination="getList" v-show="total>0"/>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <yongliaoMingxi-form-list ref="yongliaoMingxiForm" @ok="getList"></yongliaoMingxi-form-list>
    <factory-test-quality-goods ref="qualityTestingGoods" @ok="getList"> </factory-test-quality-goods>
    <add-goods-again-form @ok="getList" ref="addGoodsForm"></add-goods-again-form>
    <editRemark-form @ok="getList" ref="editRemarkForm"></editRemark-form>
    <edit-goods-form @ok="getList" ref="editGoodsForm"></edit-goods-form>
    <edit-goods-tuihui-form @ok="getList" ref="editGoodsTuihuiForm"></edit-goods-tuihui-form>
    <shengchanDoGoodsSale-form @ok="getList" ref="shengchanDoGoodsSaleForm"></shengchanDoGoodsSale-form>
    <contractual-cj-list  ref="ContractualCjList"></contractual-cj-list>
    <div class="dialog-footer" slot="footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, addAction, deleteAction, getAction, getById} from '@/api/manage'
import ChoseGxForm from "./ChoseGxForm";
import ChajieForm from "./ChajieForm";
import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";
import FactoryTestQuality from "@/views/develop/factory/factoryTestQuality/FactoryTestQuality.vue";
import YongliaoMingxiFormList from '@/views/develop/goodsSaleMaterial/module/YongliaoMingxiFormList'
import FactoryTestQualityGoods from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityGoods.vue";
import AddGoodsAgainForm from '@/views/develop/projectBaseInfo/module/AddGoodsAgainForm.vue';
import EditGoodsForm from '@/views/develop/projectBaseInfo/module/EditGoodsForm.vue';  // 引入 EditGoodsForm 组件
import EditRemarkForm from '@/views/develop/projectBaseInfo/module/EditRemarkForm.vue';
import EditGoodsTuihuiForm from '@/views/develop/projectBaseInfo/module/EditGoodsTuihuiForm.vue';
import ShengchanDoGoodsSaleForm from '@/views/develop/goodsSale/module/ShengchanDoGoodsSaleForm.vue';
import ContractualCjList from "../../projectBaseInfo/module/ContractualCjList";
export default {
    name: "AllGoodsFormList",
    dicts: ['isdo','sale_state','goods_status'],
    components: {
      FactoryTestQualityGoods,
      FactoryTestQuality,
      ChoseGxForm,
      ChajieForm,
      FpTaskFormList,
      XunjianForm,
      YongliaoMingxiFormList,
      AddGoodsAgainForm,
      EditGoodsForm, // 注册 EditGoodsForm 组件
      EditGoodsTuihuiForm,
      ShengchanDoGoodsSaleForm,
      EditRemarkForm,
      ContractualCjList
    },
    data() {
            return {
              maxHeight: (window.innerHeight * 0.9 - 260) ,
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "订单1产品明细",
                // 是否显示弹出层
                open: false,
                // 显示搜索条件
                showSearch: true,
                // 表单参数
                form: {},
                proId: "",
                proStatus: "",
                columns: [],
                projectColumns: [],
                total: 0,
                // 表单校验
                rules: {
                  goodsCode: [{ required: true, message: "产品编码不能为空", trigger: "blur" }],
                },
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    id: null,
                    code: null,
                    projectNo: null,
                    goodsName: null,
                    goodsCode: null,
                    model: null,
                    brand: null,
                    isdo: null,
                    remark: null,
                    projectId: null,
                    orderByColumn:"sort",
                    isAsc:"asc"
                },
                url: {
                    list: '/goodsSale/goodsSale/listAndFour',
                    getById: '/projectBaseInfo/projectBaseInfo/',
                },
            };
        },
        created() {
          //this.handleQuery();
        },
          methods: {
            chaijieBnt(row) {
              this.$refs.ContractualCjList.caijieOpen(row);
            },
            shengchanDoGoodsSale(row){
              this.$refs.shengchanDoGoodsSaleForm.show(row);
            },
             /** 排序触发事件 */
            handleSortChange(column, prop, order) {
              this.queryParams.orderByColumn = column.prop;
              this.queryParams.isAsc = column.order;
              this.handleQuery();
            },
            yongliaoMingxi(row){
              this.$refs.yongliaoMingxiForm.show(row);
            },
            qualityTest(row){
              this.$refs.qualityTestingGoods.showByGoodsSale(row);
            },
            taskFp(row){
              this.$refs.taskFpForm.taskFpShow(row);
            },
            chaijie(row){
              this.$refs.chaijieForm.chaijieGoods(row);
            },
            choseGy(row){
              this.$refs.choseGxForm.choseGy(row);
            },
            shedingGygx(row){
              this.open = true;
              this.proId=row.id;
              this.projectColumns=row;
              this.getList();
            },
            getList() {
                this.loading = true;
                this.queryParams.projectId = this.proId;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.columns = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
              this.queryParams.pageNum = 1;
              this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
              this.resetForm("queryForm");
              this.handleQuery();
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
          reset() {
            this.form = {
              id: null,
              remark: null,
              delFlag: null,
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null
            };
            this.resetForm("form");
           },
            qualityTesting(row){
              this.$refs.qualityTesting.show(row);
            },
            addGoods() {
              this.$refs.addGoodsForm.show(this.projectColumns);
            },
            editGoods(row) {
              this.$refs.editGoodsForm.show(row);
            },
            editRemark(row) {
              this.$refs.editRemarkForm.show(row);
            },
            editGoodsTuihui(row) {
              this.$refs.editGoodsTuihuiForm.show(row);
            },
            deleteByIdThree(row){
              const id = row.id;
              deleteAction('/goodsSale/goodsSale/deleteByIdThree/', id).then(response => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
              }).catch(() => {
              });
            },
            toAgain(row){
              const id = row.id;
              deleteAction('/goodsSale/goodsSale/toAgain/', id).then(response => {
                this.getList();
                this.$modal.msgSuccess("提交成功");
              }).catch(() => {
              });
            },
            deleteByIdFour(row){
              const id = row.id;
              deleteAction('/goodsSale/goodsSale/deleteByIdFour/', id).then(response => {
                this.getList();
                this.$modal.msgSuccess("关闭成功");
              }).catch(() => {
              });
            },
            deleteSaleFour(row){
              const id = row.id;
              deleteAction('/goodsSale/goodsSale/deleteSaleFour/', id).then(response => {
                this.getList();
                this.$modal.msgSuccess("关闭成功");
              }).catch(() => {
              });
            },
            getSummaries(param) {
              // console.log(param);
              const {columns, data} = param;
              const sums = [];
              columns.forEach((column, index) => {
                if (index === 0) {
                  sums[index] = "合计";
                  return;
                }
                const values = data.map((item) => Number(item[column.property]));
                // 只合计amount1和amount3，想合并哪些列就在这里指定
                if ( column.property === "orderCount" || column.property === "goodsTotalPrice" || column.property === "goodsTaxTotalPrice" || column.property === "pleaseBuyCount" || column.property === "fahuoShengyu" || column.property === "lockCount") {
                  sums[index] = values.reduce((prev, curr) => {
                    const value = Number(curr);
                    if (!isNaN(value)) {
                      return accAdd(prev , curr);
                    } else {
                      return prev;
                    }
                  }, 0);
                }
              });
              return sums;
            },
        }
    };
</script>

